﻿using Newtonsoft.Json;
using System;
using System.Web;
using Easy.Common;
using Easy.BLL;
using Easy.Operation;

namespace Easy.Web.Service.Data
{
    /// <summary>
    /// SaveData 的摘要说明
    /// </summary>
    public class SaveData : BaseHttpHandler
    {
        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                string data = context.Request["data"];
                string entityName = context.Request["entityname"];
                string id = context.Request["id"];
                string message;
                string saveid;

                DataService service = new DataService(entityName, CurrentUserId);
                if (string.IsNullOrEmpty(id))
                {
                    saveid = service.Create(data);
                    message = "新增成功";
                }
                else
                {
                    saveid = service.Update(data);
                    message = "修改成功";
                }

                CustomResult resultObj = new CustomResult
                {
                    ResultCode = 0,
                    ResultMessage = message,
                    ResultData = saveid
                };
                context.Response.Write(JsonConvert.SerializeObject(resultObj));
            }
            catch (CustomException ex)
            {
                context.Response.Write(JsonConvert.SerializeObject(ex.CustomResult));
            }
            catch (Exception ex1)
            {
                context.Response.Write(JsonConvert.SerializeObject(new CustomResult()
                {
                    ResultCode = -1,
                    ResultMessage = ex1.Message,
                    ResultData = ""
                }));
            }
        }
    }
}